#!/usr/bin/env python
import os
import os.path

from Ganga.Utility.logging import getLogger
from AutoD3PDMakerTaskManager import AutoD3PDMakerTaskManager
from AutoD3PDMakerUserInterface import AutoD3PDMakerUserInterface
from AutoD3PDMakerDaemon import AutoD3PDMakerDaemon
from SQLiteTaskStore import SQLiteTaskStore

logger = getLogger('AutoD3PDMaker')

## NB. Ganga keywords shouldn't be used in main routine: e.g. jobs, tasks

## logging level control of different components
#config.Logging['GangaThread'] = 'DEBUG'
config.Logging['AutoD3PDMaker']              = 'INFO'
config.Logging['AutoD3PDMakerMonitor']       = 'INFO'
config.Logging['AutoD3PDMakerReporter']      = 'INFO'
config.Logging['AutoD3PDMakerPPMonitor']     = 'INFO'
config.Logging['AutoD3PDMakerSubmittor']     = 'INFO'
config.Logging['AutoD3PDMakerPostProcessor'] = 'INFO'
config.Logging['AutoD3PDMakerDaemon']        = 'INFO'
config.Logging['AutoD3PDMakerTaskManager']   = 'INFO'
config.Logging['AutoD3PDMakerPlugin']        = 'INFO'
config.Logging['AutoD3PDMakerUserInterface'] = 'INFO'
config.Logging['SQLiteTaskStore']            = 'INFO'

## set global environmental variables
os.environ['AUTOD3PDMAKER_DQ2SETUP']  = '/project/atlas/nikhef/dq2/dq2_setup.sh.NIKHEF'
os.environ['AUTOD3PDMAKER_OUTPUTDIR'] = '/data/atlas2/hclee'
os.environ['AUTOD3PDMAKER_CODEDIR']   = '/project/atlas/nikhef/ganga/tools/AutoD3PDMaker/userarea'
os.environ['AUTOD3PDMAKER_DBDIR']     = '/project/atlas/nikhef/ganga/tools/AutoD3PDMaker/taskstore'
os.environ['AUTOD3PDMAKER_BLSITES']   = '/project/atlas/nikhef/ganga/tools/AutoD3PDMaker/blsites.txt'

## load the AutoD3PDMakerPlugin module
os.environ['AUTOD3PDMAKER_PLUGIN']    = '/project/atlas/nikhef/ganga/tools/AutoD3PDMaker/AMAPlugin.py'
execfile(os.environ['AUTOD3PDMAKER_PLUGIN'])

os.environ['AUTOD3PDMAKER_DBPATH']    = os.path.join( os.environ['AUTOD3PDMAKER_DBDIR'], 'autod3pdmaker.%s.db' % os.environ['LOGNAME'] )
os.environ['AUTOD3PDMAKER_REPORTDIR'] = '/user/%s/public_html/autod3pdmaker/report' % os.environ['LOGNAME']

## print out key configuration parameters
logger.info('AutoD3PDMaker user code dir   : %s' % os.environ['AUTOD3PDMAKER_CODEDIR'])
logger.info('AutoD3PDMaker task store dir  : %s' % os.environ['AUTOD3PDMAKER_DBDIR'])
logger.info('AutoD3PDMaker main task store : %s' % os.environ['AUTOD3PDMAKER_DBPATH'])
logger.info('AutoD3PDMaker user plugin     : %s' % os.environ['AUTOD3PDMAKER_PLUGIN'])
logger.info('AutoD3PDMaker html report dir : %s' % os.environ['AUTOD3PDMAKER_REPORTDIR'])

## initialize user-level plugin
#    _d3pdmaker_pin = AMAEventMakerPlugin(dq2setup  = os.environ['AUTOD3PDMAKER_DQ2SETUP'],
#                                         basedir   = os.environ['AUTOD3PDMAKER_OUTPUTDIR'],
#                                         codedir   = os.environ['AUTOD3PDMAKER_CODEDIR'],
#                                         reportdir = os.environ['AUTOD3PDMAKER_REPORTDIR'])

_d3pdmaker_pin = FastStructNtupleMakerPlugin(dq2setup  = os.environ['AUTOD3PDMAKER_DQ2SETUP'],
                                             basedir   = os.environ['AUTOD3PDMAKER_OUTPUTDIR'],
                                             codedir   = os.environ['AUTOD3PDMAKER_CODEDIR'],
                                             reportdir = os.environ['AUTOD3PDMAKER_REPORTDIR'])

## initiate the task manager
_d3pdmaker_mgr = AutoD3PDMakerTaskManager(SQLiteTaskStore(os.environ['AUTOD3PDMAKER_DBPATH']), _d3pdmaker_pin)

## initialize the user interface
d3pdmakeri = AutoD3PDMakerUserInterface(_d3pdmaker_mgr)

## loading new tasks from a text file into the task manager
#os.environ['AUTOD3PDMAKER_TASKLIST']  = '/localstore/hclee/autod3pdmaker/d3pdtask.list'
#if os.path.exists(os.environ['AUTOD3PDMAKER_TASKLIST']):
#    d3pdmakeri.addTasks(_d3pdmaker_mgr.getNewTasksFromFile(os.environ['AUTOD3PDMAKER_TASKLIST']))

## initialize the daemon
d3pdmakerd = AutoD3PDMakerDaemon(_d3pdmaker_mgr,_d3pdmaker_pin, os.environ['AUTOD3PDMAKER_DBDIR'])

logger.info('Use "d3pdmakerd.start()" to start up the d3pdmaker daemon')
logger.info('Use "d3pdmakeri" to manage AutoD3PDMaker tasks, e.g. "d3pdmakeri.listTasks()" to list all tasks')

## to start the maker, do d3pdmakerd.start()
## to stop the maker, do d3pdmakerd.stop()
